package com.situ.personal.service;

import java.util.List;
import java.util.Map;

import com.situ.personal.dao.DepartmentDao;
import com.situ.personal.entity.Department;
import com.situ.personal.entity.PageInfo;


public class DepartmentService {
	
	private DepartmentDao departmentDao = new DepartmentDao();
	
	public List<Department> findAll() {
		List<Department> list = departmentDao.findAll();
		return list;
	}

	public PageInfo findByPage(int pageNo, int pageSize) {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(pageNo);
		pageInfo.setPageSize(pageSize);
			
		// totalPage 
		int count = departmentDao.getTotalCount();
		System.out.println(count);
	    int totalPage = (int)Math.ceil((double)count / pageSize);
		pageInfo.setTotalPage(totalPage);
		System.out.println(totalPage);
			
		// list
		// pageNo,pageSize-->limit (pageNo-1)*pageSize,pageSize
		int offset = (pageNo - 1) * pageSize;
		//List<User> list = departmentDao.findByPage(offset, pageSize);
		List<Map<String, Object>> list = departmentDao.findByPage(offset, pageSize);
		pageInfo.setList(list);
		
		return pageInfo;
	}


	public boolean update(Department department) {
		int count = departmentDao.update(department);
		if (count == 1) {
			return true;
		} else {
			return false;
		}
	}


	public Department selectById(int id) {
		// TODO Auto-generated method stub
		return departmentDao.selectById(id);
	}


	public boolean insert(Department department) {
		int count = departmentDao.insert(department);
		if (count == 1) {
			return true;
		} else {
			return false;
		}
	}


	public boolean deleteById(int id) {
		int count = departmentDao.deleteById(id);
		if (count == 1) {
			return true;
		} else {
			return false;
		}
	}

}
